from django.conf import settings
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponseRedirect as redirect
from django.contrib.auth import authenticate
from StartGroup.models import Group
from groupInfo.forms import GroupInfoForm
import StartGroup.Constants
import Constants
import connectpeople.Constants
from groupInfo.models import Info
from  django.http import QueryDict

# render form for Group Information/ Show group Info
def groupInfoFormView(request):
    
    if connectpeople.Constants.session_group_web_address in request.session:
        group_web_addr = request.session[connectpeople.Constants.session_group_web_address]
        group_info = getGroupInfo(group_web_addr)        
        
        # Get info row if exists
        try:
            information = getInfo(group_info)
        except Info.DoesNotExist:
            information = None        
            
        if information == None:
            # If information does not already exists then render form
            information = ''
            # Group Info form initialization
            group_info_form = GroupInfoForm()
            return render_to_response("groupInfo/groupInfoForm.html",
                              {Constants.group_info_form_constant: group_info_form},
                              context_instance = RequestContext(request))
        else:
            # Else take user to group info page
            return render_to_response("groupInfo/groupInfoPage.html",
                              {'information': information},
                              context_instance = RequestContext(request))
    

# Save the form
def info(request):
    
    if connectpeople.Constants.session_group_web_address in request.session:
        group_web_addr = request.session[connectpeople.Constants.session_group_web_address]
        group_info = getGroupInfo(group_web_addr)
        
        # Get info values if exist
        try:
            information = getInfo(group_info)
        except Info.DoesNotExist:
            information = None
            
        if request.method == 'POST':
            # If info values are updated then update the same row in table
            # (Do not create new row)
            if information != None:                
                information.info_head = request.POST.__getitem__('info_head')
                information.info = request.POST.__getitem__('info')
                information.contact_info = request.POST.__getitem__('contact_info')
                information.locations_info = request.POST.__getitem__('locations_info')
                information.save()
            else:
                # Save and insert new row in table
                # Group Info form initialization
                group_info_form = GroupInfoForm(data=request.POST)            
                if group_info_form.is_valid():
                    group_info_form.save(group)
                        
    return render_to_response("groupInfo/groupInfoPage.html",
                              {'information': information},
                              context_instance = RequestContext(request))

# Edit group info form
def editGroupInfo(request):
    
    if connectpeople.Constants.session_group_web_address in request.session:
        group_web_addr = request.session[connectpeople.Constants.session_group_web_address]
        group_info = getGroupInfo(group_web_addr)
        
        # Get info values to show defaults in form
        information = getInfo(group_info)
        # Group Info form initialization
        group_info_form = GroupInfoForm()
        return render_to_response("groupInfo/groupInfoForm.html",
                              {'group_info_form': group_info_form, 'information': information},
                              context_instance = RequestContext(request))



# Helper function to obtain Group related to a group_web_address
def getGroupInfo(group_web_addr):
    return Group.objects.get(group_web_address= group_web_addr)
    
def getInfo(group_info):
    return Info.objects.get(group=group_info)